package com.youkeyi.ddy.cloud.basic.storage.stock.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.storage.stock.entity.StoStock;
import com.youkeyi.ddy.cloud.basic.storage.stock.service.StoStockService;
import com.youkeyi.ddy.cloud.common.api.basic.storage.stock.StorageStockCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.storage.stock.dto.StorageStockListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.storage.stock.dto.StorageStockListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.storage.stock.param.StorageStockCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.stock.param.StorageStockDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.stock.param.StorageStockQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.stock.param.StorageStockUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 库存 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-03-26
 */
@Service
public class StoStockController implements StorageStockCloudService {

    @Autowired
    private StoStockService stoStockService;

    @Override
    public StorageStockListCloudDto query(StorageStockQueryCloudParam param) {
        StorageStockListCloudDto dto = new StorageStockListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getStockPrimaryKeys())) {
            param.getStockPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getBarCodes())) {
            param.getBarCodes().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getBillNos())) {
            param.getBillNos().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getGoodsKeys())) {
            param.getGoodsKeys().removeAll(Collections.singleton(null));
        }

        QueryWrapper<StoStock> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getStockPrimaryKeys()), StoStock::getPrimaryKey, param.getStockPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), StoStock::getCorpKey, param.getCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getBarCodes()), StoStock::getBarCode, param.getBarCodes())
                .in(CollectionUtils.isNotEmpty(param.getBillNos()), StoStock::getBillNo, param.getBillNos())
                .in(CollectionUtils.isNotEmpty(param.getGoodsKeys()), StoStock::getGoodsKey, param.getGoodsKeys())
                .like(StringUtils.isNotBlank(param.getGoodsNameFuzzy()), StoStock::getGoodsName, param.getGoodsNameFuzzy())
                .like(StringUtils.isNotBlank(param.getKeyWordFuzzy()), StoStock::getKeyWord, param.getKeyWordFuzzy())
        ;

        Page<StoStock> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<StoStock> stockList = stoStockService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(stockList.getRecords())) {
            dto.setTotal(stockList.getTotal());

            List<StorageStockListItemCloudDto> items = new ArrayList<>();
            stockList.getRecords().forEach(stock -> {
                StorageStockListItemCloudDto item = new StorageStockListItemCloudDto();
                item.setStockPrimaryKey(stock.getPrimaryKey());
                item.setCorpKey(stock.getCorpKey());
                item.setBillNo(stock.getBillNo());
                item.setWarehouseKey(stock.getWarehouseKey());
                item.setWarehouseName(stock.getWarehouseName());
                item.setBarCode(stock.getBarCode());
                item.setGoodsKey(stock.getGoodsKey());
                item.setGoodsName(stock.getGoodsName());
                item.setKeyWord(stock.getKeyWord());
                item.setUnitKey(stock.getUnitKey());
                item.setUnitName(stock.getUnitName());
                item.setProductDate(stock.getProductDate() != null ? stock.getProductDate().getTime() : null);
                item.setExpirationDate(stock.getExpirationDate() != null ? stock.getExpirationDate().getTime() : null);
                item.setShelfKey(stock.getShelfKey());
                item.setQuantity(stock.getQuantity());
                item.setPrice(stock.getPrice());
                item.setSupplierKey(stock.getSupplierKey());
                item.setSupplierName(stock.getSupplierName());
                item.setAdvicePrice(stock.getAdvicePrice());
                item.setInStoreType(stock.getInStoreType());
                item.setState(stock.getState());
                item.setCooperationMode(stock.getCooperationMode());
                item.setOrigStockKey(stock.getOrigStockKey());
                item.setPurchaseUserKey(stock.getPurchaseUserKey());
                item.setStorageUserKey(stock.getStorageUserKey());
                item.setStorageTime(stock.getStorageTime() != null ? stock.getStorageTime().getTime() : null);
                item.setPayType(stock.getPayType());
                item.setNotes(stock.getNotes());
                item.setFromType(stock.getFromType());
                item.setCreateTime(stock.getCreateTime() != null ? stock.getCreateTime().getTime() : null);
                item.setCreateUserKey(stock.getCreateUserKey());
                item.setCreateUserName(stock.getCreateUserName());
                item.setUpdateTime(stock.getUpdateTime() != null ? stock.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(stock.getUpdateUserKey());
                item.setUpdateUserName(stock.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(StorageStockCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getCorpKey()) || StringUtils.isBlank(param.getGoodsKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        StoStock stock = new StoStock();
        stock.setPrimaryKey(SimpleUUidUtils.getUuid());
        stock.setCorpKey(param.getCorpKey());
        stock.setBillNo(param.getBillNo());
        stock.setWarehouseKey(param.getWarehouseKey());
        stock.setWarehouseName(param.getWarehouseName());
        stock.setBarCode(param.getBarCode());
        stock.setGoodsKey(param.getGoodsKey());
        stock.setGoodsName(param.getGoodsName());
        stock.setKeyWord(param.getKeyWord());
        stock.setUnitKey(param.getUnitKey());
        stock.setUnitName(param.getUnitName());
        stock.setProductDate(param.getProductDate() != null ? new Date(param.getProductDate()) : null);
        stock.setExpirationDate(param.getExpirationDate() != null ? new Date(param.getExpirationDate()) : null);
        stock.setShelfKey(param.getShelfKey());
        stock.setQuantity(param.getQuantity());
        stock.setPrice(param.getPrice());
        stock.setAdvicePrice(param.getAdvicePrice());
        stock.setInStoreType(param.getInStoreType());
        stock.setState(param.getState());
        stock.setCooperationMode(param.getCooperationMode());
        stock.setOrigStockKey(param.getOrigStockKey());
        stock.setPurchaseUserKey(param.getPurchaseUserKey());
        stock.setStorageUserKey(param.getStorageUserKey());
        stock.setStorageTime(param.getStorageTime() != null ? new Date(param.getStorageTime()) : null);
        stock.setPayType(param.getPayType());
        stock.setSupplierKey(param.getSupplierKey());
        stock.setSupplierName(param.getSupplierName());
        stock.setNotes(param.getNotes());
        stock.setFromType(param.getFromType());
        stock.setCreateUserKey(param.getCreateUserKey());
        stock.setCreateUserName(param.getCreateUserName());
        stock.setCreateTime(new Date());
        boolean save = stoStockService.save(stock);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(stock.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(StorageStockUpdateCloudParam param) {
        return null;
    }

    @Override
    public ResponseContent delete(StorageStockDeleteCloudParam param) {
        return null;
    }
}
